<?php
function parse($s){
    /*
     *
     * 1 - 2 -3-4-5-6
     * 
     */
    $parsed = array();
    $len = strlen($s);
    $stat = 1;
    $start = 0;
    $end = 0;
    for($i = 0; $i < $len;++$i){
        switch($stat){
            case 1:
                if($s[$i] == '+' || $s[$i] == '-'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'op');
                }elseif($s[$i] == '0'){
                    $stat = 3;
                    $start = $i;
                }elseif($s[$i] > '0'  && $s[$i] <= '9'){
                    $stat = 11;
                    $start = $i;
                }elseif($s[$i] == '\''){
                    $stat = 18;
                    $start = $i;
                }elseif($s[$i] == '"'){
                    $stat = 19;
                    $start = $i;
                }elseif($s[$i] == '='){
                    $stat = 22;
                    $start = $i;
                }elseif($s[$i] == '!'){
                    $stat = 25;
                    $start = $i;
                }elseif($s[$i] == '_' || ($s[$i] >= 'a' && $s[$i] <= 'z') ||($s[$i] >= 'A' && $s[$i] <= 'Z')){
                    $stat = 23;
                    $start = $i;
                }elseif($s[$i] == '['){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'l1');
                }elseif($s[$i] == '('){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'l2');
                }elseif($s[$i] == '{'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'l3');
                }elseif($s[$i] == ']'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'r1');
                }elseif($s[$i] == ')'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'r2');
                }elseif($s[$i] == '}'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'r3');
                }elseif($s[$i] == ','){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'exp');
                }elseif($s[$i] == '>' || $s[$i] == '>'){
                    $stat = 24;
                    $start = $i;
                }elseif($s[$i] == ';'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'endline');
                }elseif($s[$i] == ':'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'labelsign');
                }elseif($s[$i] == '&'){
                    $stat = 26;
                }elseif($s[$i] == '|'){
                    $stat = 27;
                }elseif($s[$i] == '^'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'bitxor');
                }elseif($s[$i] == '*'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'mul');
                }elseif($s[$i] == '/'){
                    $stat = 27;
                }elseif($s[$i] == '%'){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'rem');
                }elseif($s[$i] == '#'){
                    $stat = 28;
                }elseif($s[$i] == "\n"){
                    $parsed []= array('start' => $i,'end' => $i,'str' => substr($s,$i,1),'type' => 'endline');
                }
                break;
            case 2:
                if($s[$i] > '0'  && $s[$i] <= '9'){
                    $stat = 11;
                }elseif($s[$i] == '0'){
                    $stat = 17;
                }else{
                    $stat = 1;
                    $i = $i - 1;
                }
                if($i + 1 == $len){
                    if($stat == 11){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                    }
                }
                break;
            case 3:
                if($s[$i] >= '0'  && $s[$i] <= '7'){
                    $stat = 5;
                }elseif($s[$i] == '.'){
                    $stat = 9;
                }elseif($s[$i] == 'x' || $s[$i] == 'X'){
                    $stat = 4;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                }
                if($i + 1 == $len){
                    if($stat == 5){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'oct');
                    }elseif($stat == 9 || $stat == 4){
                        $stat = 1;
                        $end = $i - 1;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                    }
                }
                break;
            case 4:
                if(($s[$i] >= '0'  && $s[$i] <= '9') || ($s[$i] >= 'a'  && $s[$i] <= 'f') || ($s[$i] >= 'A'  && $s[$i] <= 'F')){
                    $stat = 7;
                }else{
                    $stat = 1;
                    $end = $i - 2;
                    $i = $i - 2;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                }
                if($i + 1 == $len){
                    if($stat == 7){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'hex');
                    }
                }
                break;
            case 5:
                if($s[$i] >= '0'  && $s[$i] <= '7'){
                    ;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'oct');
                }
                if($i + 1 == $len){
                    if($stat == 5){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'oct');
                    }
                }
                break;
            case 6:
                break;
            case 7:
                if(($s[$i] >= '0'  && $s[$i] <= '9') || ($s[$i] >= 'a'  && $s[$i] <= 'f') || ($s[$i] >= 'A'  && $s[$i] <= 'F')){
                    ;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'hex');
                }
                if($i + 1 == $len){
                    if($stat == 7){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'hex');
                    }
                }
                break;
            case 8:
            case 9:
                if($s[$i] >= '0'  && $s[$i] <= '9'){
                    $stat = 10;
                }else{
                    $stat = 1;
                    $end = $i - 2;
                    $i = $i - 2;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                }
                if($i + 1 == $len){
                    if($stat == 10){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                    }
                }
                break;
            case 10:
                if($s[$i] >= '0'  && $s[$i] <= '9'){
                    ;
                }elseif($s[$i] == 'E'  || $s[$i] == 'e'){
                    $stat = 13;
                }elseif($s[$i] == '+'  || $s[$i] == '-'){
                    $stat = 12;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                }
                if($i + 1 == $len){
                    if($stat == 10){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                    }elseif($stat == 12 || $stat == 13){
                        $stat = 1;
                        $end = $i - 1;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                    }
                }
                break;
             case 11:
                if($s[$i] >= '0'  && $s[$i] <= '9'){
                    ;
                }elseif($s[$i] == '+'  || $s[$i] == '-'){
                    $stat = 12;
                }elseif($s[$i] =='e' || $s[$i] =='E'){
                    $stat = 13;
                }elseif($s[$i] =='.'){
                    $stat = 9;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                }
                if($i + 1 == $len){
                    if($stat == 11){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                    }elseif($stat == 9 || $stat == 12 || $stat == 13){
                        $stat = 1;
                        $end = $i - 1;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                    }
                }
                break;
             case 12:
                if($s[$i] =='e' || $s[$i] =='E'){
                    $stat = 13;
                }else{
                    $stat = 1;
                    if($s[$i - 1] == '+' || $s[$i - 1] == '-'){
                        $end = $i - 2;
                        $i = $i - 2;
                    }else{
                        $end = $i - 1;
                        $i = $i - 1;
                    }
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                }
                if($i + 1 == $len){
                    if($stat == 13){
                        $stat = 1;
                        $end = $i - 2;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                    }
                }
                break;
             case 13:
                if($s[$i] >= '0'  && $s[$i] <= '9'){
                    $stat = 14;
                }else{
                    $stat = 1;
                    if($s[$i - 2] == '+' || $s[$i - 2] == '-'){
                        $end = $i - 2;
                        $i = $i - 2;
                    }else{
                        $end = $i - 1;
                        $i = $i - 1;
                    }
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'int');
                }
                if($i + 1 == $len){
                    if($stat == 14){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                    }
                }
                break;
             case 14:
                if($s[$i] >= '0'  && $s[$i] <= '9'){
                    ;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                }
                if($i + 1 == $len){
                    if($stat == 14){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                    }
                }
                break;
             case 15:
                break;
             case 16:
                break;
             case 17:
                 if($s[$i] == '.' ){
                    $stat = 9;
                }else{
                    $stat = 1;
                    $end = $i - 1;
                    $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                }
                if($i + 1 == $len){
                    if($stat == 14){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'float');
                    }
                }
                break;
             case 18:
                 if($s[$i] == '\''){
                     $stat = 1;
                    $end = $i;
                    $len = $end - $start - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start + 1,$len) ,'len' => $len,'type' => 'sstring');
                 }elseif($s[$i] == '\\'){
                     $stat = 20;
                 }
                 break;
             case 19:
                 if($s[$i] == '"'){
                     $stat = 1;
                    $end = $i;
                    $len = $end - $start - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start + 1,$len) ,'len' => $len,'type' => 'dstring');
                 }elseif($s[$i] == '\\'){
                     $stat = 21;
                 }
                 break;
             case 20:
                 $stat = 18;
                 break;
             case 21:
                 $stat = 19;
                 break;
             case 22:
                 if($s[$i] == '='){
                     $stat = 1;
                     $end = $i;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1) ,'type' => 'equal');
                 }else{
                     $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1),'type' => 'set');
                 }
                 break;
             case 23:
                 if($s[$i] == '_' || ($s[$i] >= 'a' && $s[$i] <= 'z') ||($s[$i] >= 'A' && $s[$i] <= 'Z') ||($s[$i] >= '0' && $s[$i] <= '9')){
                    ;
                }else{
                    $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                     $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'token');
                }
                if($i + 1 == $len){
                    if($stat == 23){
                        $stat = 1;
                        $end = $i;
                        $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start,($end - $start + 1)),'type' => 'token');
                    }
                }
                 break;
             case 24:
                 if($s[$i - 1] == '>'){
                     $type = 1;
                 }elseif($s[$i - 1] == '>'){
                     $type = 2;
                 }
                 if($s[$i] == '='){
                     $stat = 1;
                     $end = $i;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1) ,'type' => $type == 1?'ge':'le');
                 }else{
                     $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1),'type' => $type == 1?'gt':'lt');
                 }
                 break;
             case 25:
                 if($s[$i] == '='){
                     $stat = 1;
                     $end = $i;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1) ,'type' => 'ne');
                 }else{
                     $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1),'type' => 'not');
                 }
                 break;
             case 26:
                 if($s[$i] == '&'){
                     $stat = 1;
                     $end = $i;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1) ,'type' => 'and');
                 }else{
                     $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1),'type' => 'bitand');
                 }
                 break;
             case 26:
                 if($s[$i] == '|'){
                     $stat = 1;
                     $end = $i;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1) ,'type' => 'or');
                 }else{
                     $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1),'type' => 'bitor');
                 }
                 break;
             case 27:
                 if($s[$i] == '/'){
                     $stat = 28;
                 }elseif($s[$i] == '*'){
                     $stat = 29;
                 }else{
                     $stat = 1;
                     $end = $i - 1;
                     $i = $i - 1;
                    $parsed []= array('start' => $start,'end' => $end,'str' => substr($s,$start ,$end - $start + 1),'type' => 'div');
                 }
                 break;
             case 28:
                 if($s[$i] == "\n"){
                     $stat = 1;
                 }
                 break;
             case 29:
                 if($s[$i] == '*' && $s[$i + 1] == '/'){
                     $stat = 1;
                     $i = $i + 1;
                 }
                 break;
        }
    }
    return $parsed;
}

function interpret(&$parsed){
    $codes= array();
    $funcs= array();
    $stat = 1;
    foreach($parsed as $i => $t){
                if($t['type'] == 'token'){
                    switch(strtolower($t['str'])){
                        case 'if':
                            $parsed[$i]['type'] = 'if';
                            break;
                        case 'while':
                            $parsed[$i]['type'] = 'while';
                            break;
                        case 'def':
                            $parsed[$i]['type'] = 'def';
                            $parsed[$i + 1]['type'] = 'funcdef';
                            break;
                        case 'repeat':
                            $parsed[$i]['type'] = 'repeat';
                            break;
                        case 'return':
                            $parsed[$i]['type'] = 'return';
                            break;
                        case 'else':
                            $parsed[$i]['type'] = 'else';
                            break;
                        case 'elseif':
                            $parsed[$i]['type'] = 'elseif';
                            break;
                        case 'end':
                            $parsed[$i]['type'] = 'end';
                            break;
                        case 'do':
                            $parsed[$i]['type'] = 'do';
                            break;
                        case 'step':
                            $parsed[$i]['type'] = 'step';
                            break;
                        case 'then':
                            $parsed[$i]['type'] = 'then';
                            break;
                        case 'require':
                            $parsed[$i]['type'] = 'require';
                            break;
                        default:
                            if($parsed[$i + 1]['type']=='l2'){
                                $parsed[$i]['type'] = 'func';
                            }else{
                                $parsed[$i]['type'] = 'var';
                            }
                    }
                }elseif($t['type'] == 'op'){
                    if($parsed[$i + 1]['type']=='op'){
                        $parsed[$i + 1]['type'] = 'symbol';
                    }elseif($parsed[$i - 1]['type']=='l2'){
                        $parsed[$i]['type'] = 'symbol';
                    }else{
                        if($t['str'] == '+'){
                            $parsed[$i + 1]['type'] = 'add';
                        }elseif($t['str'] == '-'){
                            $parsed[$i ]['type'] = 'sub';
                        }
                    }
                }
    }
    $line = 1;
    $funcs = array();
    $labels = array();
    $vars = array();
    foreach($parsed as $i){
        if(){
            
        }
    }
}

    $str = file_get_contents('d.txt');
//    echo strlen($str);
    $t1 = microtime(true);
//    print_r(sizeof(parse(str_repeat($str,20000))));
//    print_r(sizeof(parse($str)));
//        sizeof(parse(str_repeat($str,20000)));
    $r = parse($str);
    interpret($r);
    print_r($r);
//    parse($str);
    $t2 = microtime(true);
    echo "\n";
    echo ($t2 - $t1);
    echo "\n";
    
?>